<?php

namespace App\Models\Check;

use App\Enums\Check\CheckEnum;
use App\Models\BaseModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CheckStreamModel extends BaseModel
{
    use KfUid;

    protected $table    = 'check_streams';
    protected $fillable = [
        'tradeTime',
        'orderId',
        'subject',
        'sysType',
        'orderPrice',
        'tradePrice',
        'incomePrice',
        'feePrice',
        'feeRate',
        'thirdPayNo',
        'company',
        'companyAccount',
        'payNo',
        'payChannel',
        'payChannelChild',
        'payAction',
        'checkStatus',
        'errorReason',
        'remark',
        'payPic',
        'date',
    ];

    public function getSysTypeTextAttribute(): string
    {
        return CheckEnum::SYS_TYPE_TEXT[$this->sysType] ?? '';
    }

    public function getPayChannelTextAttribute(): string
    {
        return CheckEnum::PAY_CHANNEL_TEXT[$this->payChannel] ?? '';
    }

    public function getPayActionTextAttribute(): string
    {
        if ($this->payAction == 0) {
            return '--';
        }

        return CheckEnum::PAY_ACTION_TEXT[$this->payAction] ?? '';
    }

    public function getCheckStatusTextAttribute(): string
    {
        return CheckEnum::CHECK_STATUS_TEXT[$this->checkStatus] ?? '';
    }

    /**
     * 对账成功
     * @return self
     */
    public function checkSuccess(): self
    {
        $this->checkStatus = CheckEnum::CHECK_STATUS_SUCCESS;
        $this->save();

        return $this;
    }

    /**
     * 对账失败
     * @param string $errorMsg
     * @return $this
     */
    public function checkError(string $errorReason): self
    {
        $this->checkStatus = CheckEnum::CHECK_STATUS_ERROR;
        $this->errorReason = $errorReason;
        $this->save();

        return $this;
    }

    /**
     * @return HasOne
     */
    public function signOrderRel(): HasOne
    {
        return $this->hasOne(OpenShopSignOrderModel::class, "orderNo", "orderId");
    }
}
